package com.recruit.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.recruit.common.LocalUser;
import com.recruit.common.mybatis.Page;
import com.recruit.common.util.PageUtil;
import com.recruit.dto.info.CreateOrUpdateInfoDTO;
import com.recruit.model.InfoDO;
import com.recruit.service.InfoService;
import com.recruit.vo.CreatedVO;
import com.recruit.vo.DeletedVO;
import com.recruit.vo.PageResponseVO;
import com.recruit.vo.UpdatedVO;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.autoconfigure.exception.ParameterException;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.Logger;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.core.annotation.PermissionModule;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Positive;
import java.util.Date;

@RestController
@RequestMapping("/recruit/info")
@PermissionModule(value = "新闻资讯")
public class InfoController {

    @Resource
    private InfoService infoService;

    /**
     * 新增新闻资讯
     * @return
     */
    @Logger(template = "新增新闻资讯")
    @GroupRequired
    @PermissionMeta(value = "新闻资讯")
    @PostMapping("")
    public CreatedVO create(@RequestBody @Validated CreateOrUpdateInfoDTO validator) {
        String title = validator.getTitle();
        //要保证标题唯一
        if(infoService.isTitleExists(title)){
            throw new ParameterException(8400);
        }
        InfoDO infoDO = new InfoDO();
        BeanUtil.copyProperties(validator,infoDO);
        infoDO.setUserId(LocalUser.getLocalUser().getId());
        infoService.saveOrUpdate(infoDO);
        return new CreatedVO(8000);
    }

    /**
     * 根据id更新新闻资讯信息
     *
     * @param id
     * @param validator
     * @return
     */
    @Logger(template = "更新新闻资讯信息")
    @PutMapping("/{id}")
    @GroupRequired
    @PermissionMeta(value = "更新新闻资讯信息")
    public UpdatedVO update(@PathVariable("id") @Positive(message = "{id.positive}") Integer id,
                            @RequestBody @Validated CreateOrUpdateInfoDTO validator) {
        InfoDO info = infoService.getById(id);
        if (info == null) {
            throw new NotFoundException(8300);
        }
        BeanUtil.copyProperties(validator,info);
        info.setUpdateTime(new Date());
        infoService.saveOrUpdate(info);
        return new UpdatedVO(8100);
    }

    /**
     * 根据id删除职位分类
     * @param id
     * @return
     */
    @Logger(template = "删除新闻资讯")
    @DeleteMapping("/{id}")
    @GroupRequired
    @PermissionMeta(value = "删除新闻资讯")
    public DeletedVO delete(@PathVariable @Positive(message = "{id.positive}") Integer id) {
        InfoDO infoDO = infoService.getById(id);
        if (infoDO == null) {
            throw new NotFoundException(8300);
        }
        infoService.removeById(infoDO.getId());
        infoDO.setUpdateTime(new Date());
        return new DeletedVO(8200);
    }
    /**
     * 根据状态分页查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public InfoDO get(@PathVariable(value = "id") Integer id){
        InfoDO infoDO = infoService.getById(id);
        if (infoDO == null) {
            throw new NotFoundException(8300);
        }
        return infoDO;
    }

    /**
     * 根据状态分页查询
     *
     * @param count
     * @param page
     * @param state
     * @return
     */
    @GetMapping("/page/{state}")
    public PageResponseVO<InfoDO> page(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page,
            @PathVariable(value = "state") Integer state){
        // 创建一个封装了请求参数的page对象
        Page<InfoDO> pager = new Page<>(page, count);
        // mybatis-plus查询构造器
        QueryWrapper<InfoDO> wrapper = new QueryWrapper<>();
        wrapper.eq("state", state);
        wrapper.orderByDesc("create_time");
        // 获取封装有结果集的分页对象
        IPage<InfoDO> paging = infoService.getBaseMapper().selectPage(pager, wrapper);
        return PageUtil.build(paging);
    }

    /**
     * 根据新闻资讯名称模糊查询资讯列表
     * @param count
     * @param page
     * @param title
     * @return
     */
    @GetMapping("/pageTitle/{title}")
    public PageResponseVO<InfoDO> pageKeyword(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page,
            @RequestParam String title
    ) {
        Page<InfoDO> pager = new Page<>(page, count);
        // mybatis-plus查询构造器
        QueryWrapper<InfoDO> wrapper = new QueryWrapper<>();
        // 审核通过的职位才会被查询出来
        wrapper.eq("state", 0).like("title", title);
        // 获取封装有结果集的分页对象
        IPage<InfoDO> paging = infoService.getBaseMapper().selectPage(pager, wrapper);
        return PageUtil.build(paging);
    }

}
